package com.elibaxin.ndktest

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.elibaxin.ndktest.ui.theme.NDKTestTheme
import com.elibaxin.thirdlib.NativeLib

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            NDKTestTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Column(content = {
                        Addition(result = add(3, 5))
                        Subtract(result = sub(10, 2))
                        Multiply(result = multi(2, 4))
                        Divide(result = div(16, 2))
                        NativeLibTest(NativeLib().stringFromJNI())
                        GifLibTest(test())
                    }, modifier = Modifier.padding(innerPadding))
                }
            }
        }
    }

    external fun add(a: Int, b: Int): Int
    external fun sub(a: Int, b: Int): Int
    external fun multi(a: Int, b: Int): Int
    external fun div(a: Int, b: Int): Int
    external fun test(): String

    companion object {
        /*
         * this is used to load the 'hello-jni' library on application
         * startup. The library has already been unpacked into
         * /data/data/com.example.hellojni/lib/libhello-jni.so
         * at the installation time by the package manager.
         */
        init {
            System.loadLibrary("native-lib")
        }
    }
}

@Composable
fun Greeting(result: Int, modifier: Modifier = Modifier) {
    Column {
        Addition(result = 2)
        Subtract(result = 4)
        Multiply(result = 6)
        Divide(result = 8)
    }
}

@Composable
fun Addition(result: Int, modifier: Modifier = Modifier) {
    Text(
        text = "JNI调用Native加法：$result",
        modifier = modifier
    )
}

@Composable
fun Subtract(result: Int, modifier: Modifier = Modifier) {
    Text(
        text = "JNI调用Native减法：$result",
        modifier = modifier
    )
}

@Composable
fun Multiply(result: Int, modifier: Modifier = Modifier) {
    Text(
        text = "JNI调用Native乘法：$result",
        modifier = modifier
    )
}

@Composable
fun Divide(result: Int, modifier: Modifier = Modifier) {
    Text(
        text = "JNI调用Native除法：$result",
        modifier = modifier
    )
}

@Composable
fun NativeLibTest(content: String, modifier: Modifier = Modifier) {
    Text(
        text = "Android Native Library测试：$content",
        modifier = modifier
    )
}

@Composable
fun GifLibTest(content: String, modifier: Modifier = Modifier) {
    Text(
        text = "外部gif功能so库测试：$content",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    NDKTestTheme {
        Greeting(666)
    }
}